package com.ticket.common.utils;

import java.util.Random;
import java.util.UUID;

/**
 * 生成ID工具类
 * Created by yang_dongxin on 2016/7/27.
 */
public class IDGenerator {
    public final static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9"};

    /**
     * 生成uuid
     *
     * @return
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 四位短码
     *
     * @return
     */
    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = IDGenerator.generateUUID();
        for (int i = 0; i < 4; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x24]);//0x24  36   0x3E 62
        }
        return shortBuffer.toString();
    }
    /**
     * 生成4位随机数字
     * @return
     */
    public static String getFourRandom(){
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if(randLength<4){
            for(int i=1; i<=4-randLength; i++)
                fourRandom = "0" + fourRandom  ;
        }
        return fourRandom;
    }

    /**
     * 加盐MD5加密
     * @param username
     * @param password
     * @return
     */
//    public static String passwordGenerator(String username, String password) {
//        Md5PasswordEncoder encoder = new Md5PasswordEncoder();
//        encoder.setEncodeHashAsBase64(true);
//        return encoder.encodePassword(password, username);
//    }


}
